//
//  HMStatusViewModel.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/13.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

class HMStatusViewModel: NSObject {

    static let sharedInstance: HMStatusViewModel = HMStatusViewModel()
    
    //加载微博数据
    let urlSting = "https://api.weibo.com/2/statuses/home_timeline.json"
 
    //参数
    let parameters = ["access_token":HMUserAccountViewModel.shareInstance.userAccount!.access_token!]
    
    //请求数据
    func loadStatus(loadStatusCallback: (statuses: [HMStatus]?,error: NSError?) -> ()){
    
      
        
        HMNetworkTool.sharedInstanse.GET(urlSting, parameters: parameters, progress: nil
            , success: { (task, resObject) in
//                HMPrint("获取微博数据成功了: \(resObject)")
                
                if let dict = resObject as? [String: AnyObject],let statusDict = dict["statuses"] as? [[String: AnyObject]]
                {
                //定义数字来保存数据
                    var statusModels: [HMStatus] = []
                    
                    for dict in statusDict{
                        let status = HMStatus(dict: dict)
                        
                        //模型添加到数组
                        statusModels.append(status)
                    
                    }
    loadStatusCallback(statuses: statusModels, error: nil)
                }
            }) { (task, error) in
                HMPrint("获取微博数据失败了: \(error)")
                loadStatusCallback(statuses: nil, error: error)

        }
    
    }
    
    
}

